<template>
  <view class="smt-cursor-point">
    <view
      v-if="
        popMenu &&
        (leftBottom || rightBottom || leftTop || rightTop) &&
        content.length > 0
      "
      :class="{
        'smt-fab--leftBottom': leftBottom,
        'smt-fab--rightBottom': rightBottom,
        'smt-fab--leftTop': leftTop,
        'smt-fab--rightTop': rightTop,
      }"
      class="smt-fab"
    >
      <view
        :class="{
          'smt-fab__content--left': horizontal === 'left',
          'smt-fab__content--right': horizontal === 'right',
          'smt-fab__content--flexDirection': direction === 'vertical',
          'smt-fab__content--flexDirectionStart': flexDirectionStart,
          'smt-fab__content--flexDirectionEnd': flexDirectionEnd,
          'smt-fab__content--other-platform': !isAndroidNvue,
        }"
        :style="{
          width: boxWidth,
          height: boxHeight,
          backgroundColor: styles.backgroundColor,
        }"
        class="smt-fab__content"
        elevation="5"
      >
        <view
          v-if="flexDirectionStart || horizontalLeft"
          class="smt-fab__item smt-fab__item--first flex-shrink-0"
        />
        <view
          v-for="(item, index) in content"
          :key="index"
          :class="{ 'smt-fab__item--active': isShow }"
          class="smt-fab__item flex-shrink-0"
          @click="_onItemClick(index, item)"
          :style="{
            color: item.active ? styles.selectedColor : styles.color,
          }"
        >
          <slot
            name="icon"
            :item="item"
            :index="index"
            :color="item.active ? styles.selectedColor : styles.color"
          >
            <image
              :src="item.active ? item.selectedIconPath : item.iconPath"
              class="smt-fab__item-image"
              mode="aspectFit"
            />
          </slot>
          <text class="smt-fab__item-text">{{ item.text }}</text>
        </view>
        <view
          v-if="flexDirectionEnd || horizontalRight"
          class="smt-fab__item smt-fab__item--first flex-shrink-0"
        />
      </view>
    </view>
    <view
      :class="{
        'smt-fab__circle--leftBottom': leftBottom,
        'smt-fab__circle--rightBottom': rightBottom,
        'smt-fab__circle--leftTop': leftTop,
        'smt-fab__circle--rightTop': rightTop,
        'smt-fab__content--other-platform': !isAndroidNvue,
      }"
      class="smt-fab__circle smt-fab__plus"
      :style="{ background: styles.buttonColor }"
      @click="_onClick"
    >
      <view
        class="fab-circle-icon"
        :class="{ 'smt-fab__plus--active': isShow && content.length > 0 }"
      >
        <smt-icons
          type="plusempty"
          :color="styles.iconColor"
          size="32"
        ></smt-icons>
      </view>
      <!-- <view class="fab-circle-v"  :class="{'smt-fab__plus--active': isShow && content.length > 0}"></view>
			<view class="fab-circle-h" :class="{'smt-fab__plus--active': isShow  && content.length > 0}"></view> -->
    </view>
  </view>
</template>

<script>
let platform = "other";
// #ifdef APP-NVUE
platform = uni.getSystemInfoSync().platform;
// #endif

/**
 * Fab 悬浮按钮
 * @description 点击可展开一个图形按钮菜单
 * @tutorial https://ext.dcloud.net.cn/plugin?id=144
 * @property {Object} pattern 可选样式配置项
 * @property {Object} horizontal = [left | right] 水平对齐方式
 * 	@value left 左对齐
 * 	@value right 右对齐
 * @property {Object} vertical = [bottom | top] 垂直对齐方式
 * 	@value bottom 下对齐
 * 	@value top 上对齐
 * @property {Object} direction = [horizontal | vertical] 展开菜单显示方式
 * 	@value horizontal 水平显示
 * 	@value vertical 垂直显示
 * @property {Array} content 展开菜单内容配置项
 * @property {Boolean} popMenu 是否使用弹出菜单
 * @event {Function} trigger 展开菜单点击事件，返回点击信息
 * @event {Function} fabClick 悬浮按钮点击事件
 * @event {Boolean} triggerClose 点击菜单被点击后是否关闭
 */
export default {
  name: "SmtFab",
  emits: ["fabClick", "trigger"],
  props: {
    pattern: {
      type: Object,
      default() {
        return {};
      },
    },
    horizontal: {
      type: String,
      default: "left",
    },
    vertical: {
      type: String,
      default: "bottom",
    },
    direction: {
      type: String,
      default: "horizontal",
    },
    content: {
      type: Array,
      default() {
        return [];
      },
    },
    show: {
      type: Boolean,
      default: false,
    },
    popMenu: {
      type: Boolean,
      default: true,
    },
    triggerClose: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      fabShow: false,
      isShow: false,
      isAndroidNvue: platform === "android",
      styles: {
        color: "#3c3e49",
        selectedColor: "#007AFF",
        backgroundColor: "#fff",
        buttonColor: "#007AFF",
        iconColor: "#fff",
      },
      contentWidth: "0rpx",
    };
  },
  computed: {
    contentWidthMin() {
      return "100rpx";
    },
    // 动态计算宽度
    boxWidth() {
      return this.getPosition(3, "horizontal");
    },
    // 动态计算高度
    boxHeight() {
      return this.getPosition(3, "vertical");
    },
    // 计算左下位置
    leftBottom() {
      return this.getPosition(0, "left", "bottom");
    },
    // 计算右下位置
    rightBottom() {
      return this.getPosition(0, "right", "bottom");
    },
    // 计算左上位置
    leftTop() {
      return this.getPosition(0, "left", "top");
    },
    rightTop() {
      return this.getPosition(0, "right", "top");
    },
    flexDirectionStart() {
      return this.getPosition(1, "vertical", "top");
    },
    flexDirectionEnd() {
      return this.getPosition(1, "vertical", "bottom");
    },
    horizontalLeft() {
      return this.getPosition(2, "horizontal", "left");
    },
    horizontalRight() {
      return this.getPosition(2, "horizontal", "right");
    },
  },
  watch: {
    pattern: {
      handler(val) {
        this.styles = Object.assign({}, this.styles, val);
      },
      deep: true,
    },
  },
  created() {
    this.isShow = this.show;
    if (this.top === 0) {
      this.fabShow = true;
    }
    // 初始化样式
    this.styles = Object.assign({}, this.styles, this.pattern);
    this.$nextTick(() => {
      this.computWidth();
    });
  },
  methods: {
    computWidth() {
      const query = uni
        .createSelectorQuery()
        // #ifndef MP-ALIPAY
        .in(this);
      // #endif
      // 获取容器的宽度
      let widths = null;
      query
        .selectAll(".smt-fab__item")
        .boundingClientRect((data) => {
          let leftPadding = 0;
          // #ifdef H5
          leftPadding = 17;
          // #endif
          widths = data.reduce((t, i) => t + i.width, leftPadding);
          this.contentWidth = widths * 2 + "rpx" || this.contentWidthMin;
        })
        .exec();
    },
    _onClick() {
      this.$emit("fabClick");
      if (!this.popMenu) {
        return;
      }
      this.isShow = !this.isShow;
    },
    open() {
      this.isShow = true;
    },
    close() {
      this.isShow = false;
    },
    /**
     * 按钮点击事件
     */
    _onItemClick(index, item) {
      this.$emit("trigger", {
        index,
        item,
      });
      if (this.triggerClose) {
        this.isShow = false;
      }
    },
    /**
     * 获取 位置信息
     */
    getPosition(types, paramA, paramB) {
      if (types === 0) {
        return this.horizontal === paramA && this.vertical === paramB;
      } else if (types === 1) {
        return this.direction === paramA && this.vertical === paramB;
      } else if (types === 2) {
        return this.direction === paramA && this.horizontal === paramB;
      } else {
        return this.isShow && this.direction === paramA
          ? this.contentWidth
          : this.contentWidthMin;
      }
    },
  },
};
</script>

<style lang="scss">
.smt-fab {
  position: fixed;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  justify-content: center;
  align-items: center;
  z-index: 10;
  border-radius: 45px;
  // box-shadow: $smt-shadow-base;
}

.smt-cursor-point {
  /* #ifdef H5 */
  cursor: pointer;
  /* #endif */
}

.smt-fab--active {
  opacity: 1;
}

.smt-fab--leftBottom {
  left: 30rpx;
  bottom: 60rpx;
  /* #ifdef H5 */
  left: calc(30rpx + var(--window-left));
  bottom: calc(60rpx + var(--window-bottom));
  /* #endif */
  // padding: 10px;
}

.smt-fab--leftTop {
  left: 30rpx;
  top: 60rpx;
  /* #ifdef H5 */
  left: calc(30rpx + var(--window-left));
  top: calc(60rpx + var(--window-top));
  /* #endif */
  // padding: 10px;
}

.smt-fab--rightBottom {
  right: 30rpx;
  bottom: 60rpx;
  /* #ifdef H5 */
  right: calc(30rpx + var(--window-right));
  bottom: calc(60rpx + var(--window-bottom));
  /* #endif */
  // padding: 10px;
}

.smt-fab--rightTop {
  right: 30rpx;
  top: 60rpx;
  /* #ifdef H5 */
  right: calc(30rpx + var(--window-right));
  top: calc(60rpx + var(--window-top));
  /* #endif */
  // padding: 10px;
}

.smt-fab__circle {
  position: fixed;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  justify-content: center;
  align-items: center;
  width: 100rpx;
  height: 100rpx;
  background-color: #3c3e49;
  border-radius: 50%;
  z-index: 11;
  // box-shadow: $smt-shadow-base;
}

.smt-fab__circle--leftBottom {
  left: 30rpx;
  bottom: 60rpx;
  /* #ifdef H5 */
  left: calc(30rpx + var(--window-left));
  bottom: calc(60rpx + var(--window-bottom));
  /* #endif */
}

.smt-fab__circle--leftTop {
  left: 30rpx;
  top: 60rpx;
  /* #ifdef H5 */
  left: calc(30rpx + var(--window-left));
  top: calc(60rpx + var(--window-top));
  /* #endif */
}

.smt-fab__circle--rightBottom {
  right: 30rpx;
  bottom: 60rpx;
  /* #ifdef H5 */
  right: calc(30rpx + var(--window-right));
  bottom: calc(60rpx + var(--window-bottom));
  /* #endif */
}

.smt-fab__circle--rightTop {
  right: 30rpx;
  top: 60rpx;
  /* #ifdef H5 */
  right: calc(30rpx + var(--window-right));
  top: calc(60rpx + var(--window-top));
  /* #endif */
}

.smt-fab__circle--left {
  left: 0;
}

.smt-fab__circle--right {
  right: 0;
}

.smt-fab__circle--top {
  top: 0;
}

.smt-fab__circle--bottom {
  bottom: 0;
}

.smt-fab__plus {
  font-weight: bold;
}

.fab-circle-icon {
  transform: rotate(0deg);
  transition: transform 0.3s;
  font-weight: 200;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.smt-fab__plus--active {
  transform: rotate(135deg) !important;
}

.smt-fab__content {
  /* #ifndef APP-NVUE */
  box-sizing: border-box;
  display: flex;
  /* #endif */
  flex-direction: row;
  border-radius: 100rpx;
  overflow: hidden;
  transition-property: width, height;
  transition-duration: 0.2s;
  width: 100rpx;
  border-color: #dddddd;
  border-width: 1rpx;
  border-style: solid;
  overflow: hidden;
}

.smt-fab__content--other-platform {
  border-width: 0;
  // box-shadow: $smt-shadow-base;
}

.smt-fab__content--left {
  justify-content: flex-start;
}

.smt-fab__content--right {
  justify-content: flex-end;
}

.smt-fab__content--flexDirection {
  flex-direction: column;
  justify-content: flex-end;
}

.smt-fab__content--flexDirectionStart {
  flex-direction: column;
  justify-content: flex-start;
}

.smt-fab__content--flexDirectionEnd {
  flex-direction: column;
  justify-content: flex-end;
}

.smt-fab__item {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100rpx;
  opacity: 0;
  transition: opacity 0.2s;
  padding: 0 14rpx;
  &:last-child {
    padding: 0;
    width: 100rpx;
  }
}

.smt-fab__item--active {
  opacity: 1;
}

.smt-fab__item-image {
  width: 40rpx;
  height: 40rpx;
  margin-bottom: 8rpx;
}

.smt-fab__item-text {
  font-size: 20rpx;
  line-height: 28rpx;
  margin-top: 4rpx;
}

.smt-fab__item--first {
  width: 100rpx;
}
</style>
